package c03.thread.api.join;

import lombok.extern.slf4j.Slf4j;
import util.Sleeper;

/**
 * 有时效的等够时间 join(ms)
 */
@Slf4j(topic = "c.JoinAndSleepTest3")
public class JoinAndSleepTest3 {
    static int r = 0;

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            Sleeper.sleep(2);
            r = 10;
        });

        long start = System.currentTimeMillis();
        t1.start();

        log.debug("join begin");
        t1.join(1500); // 没等够时间
        long end = System.currentTimeMillis();
        log.debug("r = {}， cost = {}", r, end - start); // [17:36:01.657] r = 0， cost = 1502
        t1.join(1500); // 等够了时间，但并不会等待1500ms，而是当t1线程执行完毕就返回到调用线程（以t1线程实际执行时间为准）
        end = System.currentTimeMillis();
        log.debug("r = {}， cost = {}", r, end - start); // [17:36:02.155] r = 10， cost = 2000
    }

}